<?php
include 'inner/init.php';
protect_page();

if(empty($_POST) === false){
    $required_fields = array('current_password', 'password', 'password2');
    foreach($_POST as $key=>$value){
        if (empty($value) && in_array($key, $required_fields) === true){
            $error[] = 'Fields marked with an asterisk must be filled!';
            break 1;
        }
}
if(md5($_POST['current_password']) === $user_data['password']){
    if(trim($_POST['password']) !== trim($_POST['password2'])){
	$error[] = 'Your new password do no match';
    } else if(strlen($_POST['password']) < 6){
	$error[] = 'Your password must be at least 6 characters';
    }
} else{
    $error[] = 'Your current password is incorrect';
}
    
}

include 'includes/overall/header.php';
?>
            
 <h1>Change Password</h1>
 
 <?php
 if(isset($_GET['success']) && empty($_GET['success'])){
    echo 'Your password has been changed!';
 } else{
 if(empty($_POST) === false && empty($error) === true){
    change_password($session_user_id, $_POST['password']);
    header('Location: repass.php?success');
 } else if(empty($error) === false) {
    echo output_error($error);
 }
 ?>
 
 <form action="" method="post">
    <ul>
	<li>
	    Current password*:<br>
	    <input type="password" name="current_password">
	</li>
	<li>
	    New password*:<br>
	    <input type="password" name="password">
	</li>
	<li>
	    New password*:<br>
	    <input type="password" name="password2"
	</li>
	<li>
	    <input type="submit" value="Change password">
	</li>
	
    </ul>
    
    
 </form>

<?php
 }
include 'includes/overall/footer.php'; ?>   